package com.dongdongshop.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.*;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbGoodsService;
import com.dongdongshop.util.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.el.stream.Stream;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Service
@com.alibaba.dubbo.config.annotation.Service
public class TbGoodsServiceImpl implements TbGoodsService {
//    @Reference  直接注入seller_service 服务就可以使用seller_service中的注解
//    private TbBrandService tbBrandService;
    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbItemMapper tbItemMapper;
    @Autowired
    private TbBrandMapper tbBrandMapper;
    @Autowired
    private TbItemCatMapper tbItemCatMapper;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Override
    public void addAll(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,String msg) {
        tbGoodsMapper.insertSelective(tbGoods);
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        tbGoodsDescMapper.insertSelective(tbGoodsDesc);
        //商品item
        List<String> list  = (List<String>) JSONObject.parse(msg);
        list.stream().forEach(s -> {
            try {
            TbItem item = new TbItem();
            item.setTitle(tbGoods.getGoodsName());
            item.setSellerId(tbGoods.getSellerId());
            item.setSellPoint(tbGoods.getCaption());
            item.setCategoryid(tbGoods.getCategory3Id());
            item.setCreateTime(new Date());
            item.setUpdateTime(new Date());
            item.setGoodsId(tbGoods.getId());
            item.setImage(tbGoods.getSmallPic());
            TbBrand brand = tbBrandMapper.selectByPrimaryKey(tbGoods.getBrandId());
            item.setBrand(brand.getName());
            TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(tbGoods.getCategory3Id());
            item.setCategory(tbItemCat.getName());
            item.setSeller(brand.getName());
            //解析字符串
                String[] split = s.split("/");
                item.setSpec(split[0]);
                item.setPrice(BigDecimal.valueOf(Double.parseDouble(split[1])));   //
                item.setNum(Integer.parseInt(split[2]));                              //
                tbItemMapper.insertSelective(item);
            }catch (Exception e){
                System.out.println("成功");
            }
        });
    }
    //查询goods表
    @Override
    public List<TbGoods> fildGoods() {
         List<TbGoods> tbGoods1 =tbGoodsMapper.selectByExample(null);
         return tbGoods1;
    }
    @Override
    public TbGoods selectByGoodsId(Long goodsId) {
        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(goodsId);
        return tbGoods;
    }
    @Override
    public PageBean getGoods(TbGoods goods, Integer pageNum, Integer pageSize) {
//利用PageHelper进行分页
        PageHelper.startPage(pageNum,pageSize);
        //查询出的数据放置在PageInfo对象里面
        List<TbGoods> goodsList = tbGoodsMapper.selectGoods(goods);
        PageInfo pageInfo = new PageInfo(goodsList);
        //进行PageBean的装载
        return PageBean.buildPageBean(pageNum,pageSize,pageInfo.getPages(),pageInfo.getTotal(),pageInfo.getList());
    }
    @Override
    public void toexamBatch(Long[] ids, String num) {
        rocketMQTemplate.convertAndSend("ids_topic",ids);
        for (int i = 0; i < ids.length; i++) {
            TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(ids[i]);
                tbGoods.setAuditStatus(num);
                tbGoodsMapper.updateByPrimaryKeySelective(tbGoods);
        }
    }
    @Override
    public void deleteBatch(Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            tbGoodsMapper.deleteByPrimaryKey(ids[i]);
        }
    }
}
